<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/17
 * Time: 15:02
 */

namespace Admin\Controller;

class PaperController extends BaseController{

    public function index(){
        $Paper = D('Paper');
        $PaperList = $Paper->getAll();
        for($i=0;$i<count($PaperList);$i++){
            $PaperList[$i][addtime] = date("Y-m-d",$PaperList[$i][addtime]);
        }
        $this->assign('PaperList',$PaperList);
        $this->display();
    }


    public function add(){
        if (!IS_POST) {
            $this->display();
        } else {
            $data['name'] = $_POST['name'];
            $data['addtime'] = strtotime('now');
            $data['sort'] = $_POST['sort'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Paper = D ('Paper');
            $flag = $Paper->addPaper($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }

    public function edit(){
        if(!IS_POST){
            $data['id'] = $_GET['id'];
            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->error(\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Paper = D('Paper');
            $P = $Paper->getPaperById($data);

            if (empty($P)) $this->error('不存在该记录');
            $this->assign('P',$P);
            $this->display();
        }else{
            $this->isPost();//验证是否是post方式提交数据
            $data['id'] = $_POST['id'];
            $data['name'] = $_POST['name'];
            $data['addtime'] = strtotime('now');
            $data['sort'] = $_POST['sort'];


            if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($data)) !== '000000')
                $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
            $Paper = D('Paper');
            $flag = $Paper->editPaper($data);
            if ($flag === '000000')
                $this->out('RESULT_OK','',__CONTROLLER__);
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        }
    }


    public function del(){
        $id = $_REQUEST['id'];
        if (($flag = \Api\Libs\HttpVerifyUtil::verifyFormParamNotNull($id)) !== '000000')
            $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
        $Paper = D ('Paper');
        $flag = $Paper->getDelPaper($id);
        if ($flag !== false)
            $this->out('RESULT_OK','',__CONTROLLER__);
        $this->out('RESULT_FAIL',\Api\Libs\ExplainRespCodeUtil::explain($flag));
    }


}